package com.gz.logistics.library.listener;

import android.util.Log;

import com.gz.logistics.library.pojo.OrderInfo;
import com.gz.logistics.library.pojo.UserInfo;
import com.gz.logistics.library.util.Constants;
import com.gz.logistics.library.util.CrashHandler;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * author wanner1991
 */
public abstract class HomeOrderListener extends BaseListener {
    private static  final  String TAG = HomeOrderListener.class.getSimpleName();

    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
        try {
            int status = response.getInt(Constants.REQ_STATUS);
            String msg = response.getString(Constants.REQ_MSG);
            switch(status){
                case Constants.REQ_OK:
                    JSONArray jsonArray = response.getJSONArray(Constants.REQ_RESULT);
                    onResponseSuccess(OrderInfo.parse(jsonArray));
                    break;
                case Constants.REQ_NO_DISTRIBUTE:
                    onNoDistribute();
                    break;
                case Constants.REQ_NO_CONDITION_SATISFIED_ORDERS:
                    onNoConditionSatisfiedOrder();
                    break;
                case Constants.REQ_THIS_ORDER_IS_EMPTY:
                    onDataError();
                    break;
                default:
                    onResponseFailed();
                    break;
            }

        } catch (JSONException e) {
            onFailure(statusCode, headers, e, new JSONObject());
        }
    }


    @Override
    public void onOffline() {

    }

    @Override
    public void onNoMoreData() {

    }
    @Override
    public void onFailure(int statusCode, Header[] headers,
                          Throwable throwable, JSONObject errorResponse) {
        CrashHandler.e(throwable);
        onResponseFailed();
    }

    public abstract void onResponseSuccess(List<OrderInfo> orderInfoList);

    /**
     * 没有指派订单
     */
    public abstract void onNoDistribute();

    /**
     * 没有查询到满足条件的订单
     */
    public abstract void onNoConditionSatisfiedOrder();

    /**
     * 数据异常
     */
    public abstract void onDataError();

}
